package dao.implementation;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Base64;
import java.util.HashMap;

import dao.UserDAO;
import data.User;
import utils.JDBCUtil;

public class UserDAOImplementation implements UserDAO
{
	
	@Override
	public User selectByID(int user_id)
	{
		try
		{
			String condition = UserDAO.COLUMN_NAME_ID + "='" + user_id + "'";
			ResultSet set = JDBCUtil.selectRow(UserDAO.TABLE_NAME, condition);
			
			//如果id不存在，set.first()自然会出错
			if (set == null)
				return null;
			
			String base64_username = set.getString(UserDAO.COLUMN_NAME_USERNAME);
			String base64_password = set.getString(UserDAO.COLUMN_NAME_PASSWORD);
			
			set.close();
			return new User
					(
					user_id,
					Base64.getDecoder().decode(base64_username).toString(),
					Base64.getDecoder().decode(base64_password).toString()
					);
		}
		catch (SQLException e){e.printStackTrace();}
		
		return null;
	}
	
	
	/**
	 * 检查是否已存在某用户
	 * @throws SQLException 
	 * */
	@Override
	public User selectByUsername(String username)
	{
		if (username == null
			|| username.equals("")
			|| username.length() == 0)
			return null;
		
		String base64_username = Base64.getEncoder().encodeToString(username.getBytes());
		try
		{
			String condition = UserDAO.COLUMN_NAME_USERNAME + "='" + base64_username + "'";
			ResultSet set = JDBCUtil.selectRow(UserDAO.TABLE_NAME, condition);
			if (set == null)
				return null;
			
			int id = set.getInt(UserDAO.COLUMN_NAME_ID);
			String base64_password = set.getString(UserDAO.COLUMN_NAME_PASSWORD);
			set.close();
			
			return new User
					(
					id,
					username,
					Base64.getDecoder().decode(base64_password).toString()
					);
		
		}
		catch (SQLException e){e.printStackTrace();}
		
		return null;
	}
	

	@Override
	public boolean insert(User user)
	{
		if (user == null)
			return false;
		if (this.selectByUsername(user.getUsername()) != null)
			return false;
		
		JDBCUtil.insert
				(
				UserDAO.TABLE_NAME,
				String.valueOf(user.getID()),
				user.getNickname(),
				user.getBase64Username(),
				user.getBase64Password()
				);

		return true;
	}

	@Override
	public boolean insert(String username, String password)
	{
		if (username == null || username.equals(""))
			return false;
		if (password == null || password.equals(""))
			return false;
		
		if (this.selectByUsername(username) != null)
			return false;
		
		String base64_username = Base64.getEncoder().encodeToString(username.getBytes());
		String base64_password = Base64.getEncoder().encodeToString(password.getBytes());
		
		HashMap<String, String> map = new HashMap<String, String>();
		map.put(UserDAO.COLUMN_NAME_USERNAME, base64_username);
		map.put(UserDAO.COLUMN_NAME_PASSWORD, base64_password);
		JDBCUtil.insert(UserDAO.TABLE_NAME, map);

		return true;
	}
	
	
	@Override
	public void update(User user)
	{
		HashMap<String, String> map = new HashMap<String, String>();
		map.put(UserDAO.COLUMN_NAME_ID, String.valueOf(user.getID()));
		map.put(UserDAO.COLUMN_NAME_NICKNAME, user.getNickname());
		map.put(UserDAO.COLUMN_NAME_USERNAME, user.getBase64Username());
		map.put(UserDAO.COLUMN_NAME_PASSWORD, user.getBase64Password());
		
		String condition = UserDAO.COLUMN_NAME_ID + "='" + user.getID() + "'";
		JDBCUtil.update(COLUMN_NAME_ID, map, condition);
	}
	
	
	@Override
	public void update(int user_id, String column_name, String value)
	{
		String condition = UserDAO.COLUMN_NAME_ID
				+ "='"
				+ user_id
				+ "'";
		JDBCUtil.updateOneValue(UserDAO.TABLE_NAME, column_name, value, condition);
	}
	
	
	@Override
	public boolean hasUser(int user_id)
	{
		if (this.selectByID(user_id) == null)
			return false;
		return true;
	}

	
	@Override
	public boolean hasUser(String username)
	{
		if (this.selectByUsername(username) == null)
			return false;
		return true;
	}
	
	
	
	/**
	 * 验证用户名与密码是否匹配
	 * @throws SQLException 
	 * */
	@Override
	public boolean checkUser(String username, String password)
	{
		/*
		User user = this.selectByUsername(username);
		if (password.equals(user.getPassword()))
			return true;
		return false;
		*/
		String base64_username = Base64.getEncoder().encodeToString(username.getBytes());
		String base64_password = Base64.getEncoder().encodeToString(password.getBytes());
		
		String condition1 = UserDAO.COLUMN_NAME_USERNAME
				+ "='" 
				+ base64_username
				+ "'";
		String condition2 = UserDAO.COLUMN_NAME_PASSWORD
				+ "='" 
				+ base64_password
				+ "'";
		ResultSet set = JDBCUtil.selectRow(UserDAO.TABLE_NAME, condition1, condition2);
		if (set == null)
			return false;
		return true;
	}

	




	


	
	
	
	
}







